package model;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class IngredientDAO {
    // 添加原材料
    public void addIngredient(Ingredient ingredient) throws SQLException {
        try (Connection connection = DatabaseConnection.getConnection()) {
            String query = "INSERT INTO Ingredients (StoreID, Name, Type, Price) VALUES (?, ?, ?, ?)";
            PreparedStatement statement = connection.prepareStatement(query);
            statement.setInt(1, ingredient.getStoreID());
            statement.setString(2, ingredient.getName());
            statement.setString(3, ingredient.getType());
            statement.setDouble(4, ingredient.getPrice());
            statement.executeUpdate();
        }
    }

    // 删除原材料
    public void deleteIngredient(int ingredientID) throws SQLException {
        try (Connection connection = DatabaseConnection.getConnection()) {
            String query = "DELETE FROM Ingredients WHERE IngredientID = ?";
            PreparedStatement statement = connection.prepareStatement(query);
            statement.setInt(1, ingredientID);
            statement.executeUpdate();
        }
    }

    // 查询某门店的所有原材料
    public List<Ingredient> getIngredientsByStoreID(int storeID) throws SQLException {
        List<Ingredient> ingredients = new ArrayList<>();
        try (Connection connection = DatabaseConnection.getConnection()) {
            String query = "SELECT * FROM Ingredients WHERE StoreID = ?";
            PreparedStatement statement = connection.prepareStatement(query);
            statement.setInt(1, storeID);
            ResultSet resultSet = statement.executeQuery();

            while (resultSet.next()) {
                ingredients.add(new Ingredient(
                        resultSet.getInt("IngredientID"),
                        resultSet.getInt("StoreID"),
                        resultSet.getString("Name"),
                        resultSet.getString("Type"),
                        resultSet.getDouble("Price")
                ));
            }
        }
        return ingredients;
    }
}
